package com.sunkyc.framework.system.service;

import com.sunkyc.commons.contants.GlobContants;
import com.sunkyc.framework.utils.CaptchaUtils;
import com.sunkyc.framework.system.CacheKey.AuthroticationCacheKeys;
import com.sunkyc.commons.entity.Captcha;
import com.sunkyc.framework.utils.RedisUtils;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.concurrent.TimeUnit;

@Service
@RequiredArgsConstructor
public class CaptchaService {
    private final RedisUtils redisUtils;

    public Captcha create(){
        Captcha captcha = CaptchaUtils.createCaptcha();
        captchaToRedis(captcha.getCode(),captcha.getUuid());
        return captcha;
    }


    /**
     * 生成的验证码保存到redis
     * @param code
     * @param uuid
     */
    private void captchaToRedis(String code,String uuid){
        redisUtils.setCacheObject(
                AuthroticationCacheKeys.CAPTHCA_CACHE_KEY+":"+uuid,
                code, GlobContants.CAPTCHA_CACHE_TIME,TimeUnit.MINUTES);
    }



}
